package com.dreamtour.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dreamtour.entity.Administrators;
import com.dreamtour.entity.Advertisements;
import com.dreamtour.service.IAdministratorsService;
import com.dreamtour.service.IAdvertisementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/advertisements")
public class AdvertisementsController {
    @Autowired
    private IAdvertisementsService advertisementsService;

    @RequestMapping("/getAll")
    public List<Advertisements> getAll() {
        List<Advertisements> list = advertisementsService.list();
        return list;
    }

    @RequestMapping("/getByPage")
    public IPage<Advertisements> getByPage(Integer pageNum, Integer pageSize) {
        IPage<Advertisements> page = advertisementsService.page(new Page<>(pageNum, pageSize));
        return page;
    }

    @RequestMapping("/getByName")
    public List<Advertisements> getByName(String adName) {
        QueryWrapper<Advertisements> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("adName", adName);
        List<Advertisements> list = advertisementsService.list(queryWrapper);
        return list;
    }

    @GetMapping("/filter")
    public ResponseEntity<Map<String, Object>> filterUsers(
            @RequestParam(required = false) String adStatus) {

        QueryWrapper<Advertisements> queryWrapper = new QueryWrapper<>();

        // 添加验证状态筛选条件
        if (adStatus != null && !adStatus.isEmpty()) {
            queryWrapper.eq("adStatus", adStatus);
        }

        // 查询符合条件的旅行社
        List<Advertisements> filteredUsers = advertisementsService.list(queryWrapper);

        // 准备返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("advertisements", filteredUsers);

        return ResponseEntity.ok(response);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody Advertisements advertisements) {
        boolean b = advertisementsService.updateById(advertisements);
        return b;
    }

    @RequestMapping("/updateByBatch")
    public boolean updateByBatch(@RequestBody List<Advertisements> advertisementsList) {
        boolean b = advertisementsService.updateBatchById(advertisementsList);
        return b;
    }

    @RequestMapping("/getAdvertisementCountByMonth")
    public ResponseEntity<List<Map<String, Object>>> getAdvertisementsCountByMonth() {
        List<Map<String, Object>> monthlyCounts = advertisementsService.countAdvertisementsByMonth();

        // 直接返回原始的统计数据列表
        return ResponseEntity.ok(monthlyCounts);
    }


    @RequestMapping("/getAdvertisementIncomeByMonth")
    public ResponseEntity<List<Map<String, Object>>> getAdvertisementIncomeByMonth() {
        List<Map<String, Object>> monthlyIncomes = advertisementsService.getAdvertisementIncomeByMonth();

        // 直接返回原始的统计数据列表
        return ResponseEntity.ok(monthlyIncomes);
    }

}
